#include "TIMER5.h"

void Timer5_Init(void)
{
    RCC->APB1ENR |= RCC_APB1ENR_TIM5EN;
    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;

    GPIOA->CRL |= GPIO_CRL_MODE1;
    GPIOA->CRL |= GPIO_CRL_CNF1_1;
    GPIOA->CRL &= ~GPIO_CRL_CNF1_0;

    TIM5->PSC = 7199;
    TIM5->ARR = 99;

    // TIM5->EGR |= TIM_EGR_CC2G;
    TIM5->CCMR1 &= ~TIM_CCMR1_CC2S;
    TIM5->CCMR1 |= TIM_CCMR1_OC2M_2;
    TIM5->CCMR1 |= TIM_CCMR1_OC2M_1;
    TIM5->CCMR1 &= ~TIM_CCMR1_OC2M_0;
    
    TIM5->CCR2 = 50;

    TIM5->CCER &= ~TIM_CCER_CC2P;
    TIM5->CCER |= TIM_CCER_CC2E;

}

void Timer5_Start(void)
{
    TIM5->CR1 |= TIM_CR1_CEN;
}

void Timer5_Stop(void)
{
    TIM5->CR1 &= ~TIM_CR1_CEN;
}

void Timer5_SetDuty(uint8_t duty)
{
    TIM5->CCR2 = duty;
}
